#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

char strBuffer[200]={0};
char g_ReadyFlag = 0; 

void * FunWrite( void * arg )
{
    while (1)
    {
        if ( g_ReadyFlag == 0 )
        {
            fgets( strBuffer, sizeof(strBuffer), stdin );
            g_ReadyFlag = 1;
        }
    }

}


void * FunRead( void * arg )
{
    while (1)
    {
        if ( g_ReadyFlag == 1 )
        {
            printf( "Read from Buffer: %s\n", strBuffer );
            g_ReadyFlag = 0;
        }
    }

}


int main(int argc, char const *argv[])
{
    pthread_t ptWrite;
    pthread_t ptRead;

    pthread_create( &ptWrite, NULL, FunWrite, NULL );
    pthread_create( &ptRead,  NULL, FunRead,  NULL );

    pthread_join( ptWrite, NULL );
    pthread_join( ptRead,  NULL );

    return 0;
}


/*
程序演示步骤:
第一步, 不加 g_ReadyFlag , 运行状态混乱
第二步: 增加 g_ReadyFlag , 运行状态貌似正常, 但是有隐患, 需要加互斥锁
*/